package com.yztc.administrator.meishi.utils;

import android.os.Build;
import android.os.Environment;
import android.os.StatFs;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 类描述:
 * 创建人:一一哥
 * 创建时间:16/8/25 14:31
 * 备注:
 */
public class SDUtil {

    public static final String STORAGE_PATH = "1607";

    //判断外部存储设备是否已挂载
    public static boolean isMounted() {
        //得到外部存储设备中DCIM这个公共文件夹的路径
        //Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    //获取SD根路径
    public static String getSDPath() {
        if (isMounted()) {
            return Environment.getExternalStorageDirectory().getAbsolutePath();
        }
        return null;
    }

    //计算总大小
    public static long getSDTotalSize() {
        if (isMounted()) {
            //获取某个文件的大小.
            StatFs stat = new StatFs(getSDPath());
            //判断API的版本.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                //获取总大小的方法
                // stat.getTotalBytes();
                //机械硬盘---->盘片--->扇区
                //磁道;机械臂;
                //获取块的数量
                //int blockCount = stat.getBlockCount();
                long blockCount = stat.getBlockCountLong();
                //获取块的大小
                //int blockSize = stat.getBlockSize();
                long blockSize = stat.getBlockSizeLong();
                return blockCount * blockSize / 1024 / 1024;
            }
        }
        return 0;
    }

    //计算可用空间
    public static long getSDFreeSize() {
        if (isMounted()) {
            //获取某个文件的大小.
            StatFs stat = new StatFs(getSDPath());
            //判断API的版本.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                //获取可用空间总大小的方法
                //stat.getAvailableBytes();
                //机械硬盘---->盘片--->扇区
                //磁道;机械臂;
                //获取块的数量
                //int blockCount = stat.getBlockCount();
                long blockCount = stat.getAvailableBlocksLong();
                //获取块的大小
                //int blockSize = stat.getBlockSize();
                long blockSize = stat.getBlockSizeLong();
                return blockCount * blockSize / 1024 / 1024;
            }
        }
        return 0;
    }

    //存储数据
    public static boolean saveData(String fileName, byte[] data) {
        if (isMounted()) {
            String path = getSDPath() + File.separator + STORAGE_PATH;
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }

            try {
                FileOutputStream fileOut = new FileOutputStream(new File(file, fileName));
                fileOut.write(data);
                fileOut.close();
                return true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    //读取信息
    public static byte[] readData(String fileName) {
        if (isMounted()) {
            String path = getSDPath() + File.separator + STORAGE_PATH;
            File file = new File(path);
            if (file.exists()) {
                try {
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    FileInputStream fileIn = new FileInputStream(new File(path, fileName));
                    int len = 0;
                    byte[] buffer = new byte[1024];
                    while ((len = fileIn.read(buffer)) != -1) {
                        out.write(buffer, 0, len);
                        out.flush();
                    }

                    fileIn.close();
                    byte[] data = out.toByteArray();
                    out.close();
                    return data;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
